<template>
  <el-dialog
    :model-value="dialogVisible"
    :title="dialogTitle"
    width="40%"
    @close="handleClose"
  >
    <el-form ref="formRef" :model="form" label-width="90px" :rules="rules">
      <el-form-item label="金额" prop="money" v-if="dialogTitle === '添加产品'">
        <el-input v-model="form.money" type="number" />
      </el-form-item>
      <el-form-item
        label="年利率"
        prop="rate"
        v-if="dialogTitle === '添加产品'"
      >
        <el-input v-model="form.rate" />
      </el-form-item>
      <el-form-item label="期限" prop="year" v-if="dialogTitle === '添加产品'">
        <el-input v-model="form.year" type="number" />
      </el-form-item>
      <el-form-item label="数量" prop="count" v-if="dialogTitle === '添加产品'">
        <el-input v-model="form.count" type="number" />
      </el-form-item>
      <el-form-item
        label="限购份数"
        prop="buy_max"
        v-if="dialogTitle === '添加产品'"
      >
        <el-input v-model="form.buy_max" type="number" />
      </el-form-item>
      <el-form-item label="引擎" prop="engine">
        <el-input v-model="form.engine" type="number" />
      </el-form-item>
      <el-form-item label="开始时间" v-if="dialogTitle === '添加产品'">
        <el-date-picker
          v-model="form.time_from"
          type="datetime"
          value-format="YYYY-MM-DD HH:mm:ss"
          placeholder="Select date and time"
        />
      </el-form-item>
      <el-form-item label="结束时间" v-if="dialogTitle === '添加产品'">
        <el-date-picker
          v-model="form.time_end"
          type="datetime"
          value-format="YYYY-MM-DD HH:mm:ss"
          placeholder="Select date and time"
        />
      </el-form-item>
      <el-form-item label="是否有效" v-if="dialogTitle === '添加产品'">
        <el-switch v-model="form.is_effect" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { defineEmits, ref, defineProps, watch } from 'vue'
import { addGoods, editGoods } from '@/api/goods'
import i18n from '@/i18n'
import { ElMessage } from 'element-plus'

const props = defineProps({
  dialogTitle: {
    type: String,
    default: '',
    required: true
  },
  dialogTableValue: {
    type: Object,
    default: () => {}
  }
})

const formRef = ref(null)

const form = ref({
  id: '',
  money: '',
  rate: '',
  year: '',
  count: '',
  buy_max: '',
  engine: '',
  time_from: '',
  time_end: '',
  is_effect: true
})

const rules = ref({
  money: [{ required: true, message: '请输入金额', trigger: 'blur' }],
  rate: [{ required: true, message: '请输入年利率', trigger: 'blur' }],
  year: [{ required: true, message: '请输入期限', trigger: 'blur' }],
  count: [{ required: true, message: '请输入数量', trigger: 'blur' }],
  buy_max: [{ required: true, message: '请输入限购份数', trigger: 'blur' }],
  engine: [{ required: true, message: '请输入决策引擎', trigger: 'blur' }]
})

watch(
  () => props.dialogTableValue,
  () => {
    form.value = props.dialogTableValue
  },
  { deep: true, immediate: true }
)

const emits = defineEmits(['update:modelValue', 'initGoodsList'])
const handleClose = () => {
  emits('update:modelValue', false)
}
const handleConfirm = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      form.value.money = parseInt(form.value.money)
      form.value.year = parseInt(form.value.year)
      form.value.count = parseInt(form.value.count)
      form.value.buy_max = parseInt(form.value.buy_max)
      form.value.engine = parseInt(form.value.engine)
      form.value.rate = parseFloat(form.value.rate)
      form.value.id = parseInt(form.value.id)
      props.dialogTitle === '添加产品'
        ? await addGoods(form.value)
        : await editGoods(form.value.id, form.value.engine)
      ElMessage({
        message: i18n.global.t('message.updateSuccess'),
        type: 'success'
      })
      emits('initGoodsList')
      handleClose()
    } else {
      console.log('error submit!!')
      return false
    }
  })
}
</script>

<style lang="scss" scoped>
:deep(.el-input__prefix-inner) {
  align-items: center;
}
:deep(.el-col-2) {
  display: flex;
  justify-content: center;
}
</style>
